<?php


namespace app\index\controller;


use think\App;
use think\Controller;

class AliYunDrive extends Controller
{

    protected $accessToken;
    protected $nickName;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->accessToken = '';
        $this->nickName    = '';
    }

    /**
     * 阿里云盘签到任务
     * @throws \Exception
     */
    public function index()
    {
        $refreshToken = input('get.token');
        if (strlen($refreshToken) !== 32) {
            $this->result('', 100, 'token参数长度错误', 'json');
        }
        // 从缓存中获取
        $key       = 'ali_yun_drive:access_token:' . $refreshToken;
        $keyName   = 'ali_yun_drive:name:' . $refreshToken;
        $cacheData = cache($key);
        if (!$cacheData) {
            $result = $this->refreshToken($refreshToken);
            if (isset($result['code'])) {
                // 获取失败
                $this->result('', 100, "【获取TOKEN错误】" . $result['message'], 'json');
            }
            if (isset($result['access_token'])) {
                // 获取成功
                // token
                $this->accessToken = $result['access_token'];
                cache($key, $result['access_token'], $result['expires_in']);
                // 昵称
                $this->nickName = $result['nick_name'];
                cache($keyName, $result['nick_name'], $result['expires_in']);
            }
        } else {
            $this->accessToken = $cacheData;
        }
        // 签到
        $result = $this->sign_in_list();
        if (isset($result['success'])) {
            commonSendMessage("【阿里云盘】签到提醒：用户【" . $this->nickName . '】在' . date('Y年m月d日') . "签到成功，本月共签到：" . $result['result']['signInCount'] . "次。");
        } else {
            dump($result['message']);
        }
        // 领取奖励
        $result = $this->sign_in_reward();
        if (isset($result['success'])) {
            commonSendMessage("【阿里云盘】奖励领取提醒：用户【" . $this->nickName . '】在' . date('Y年m月d日') . "领取奖励成功，奖励明细：" . $result['result']['notice'] . "。");
        } else {
            dump($result['message']);
        }
        $this->result('', 200, "【阿里云盘】任务执行完成：用户【" . $this->nickName . "】", 'json');
    }


    /**
     * 签到
     * @return mixed
     * @throws \Exception
     */
    private function sign_in_list()
    {
        $url   = 'https://member.aliyundrive.com/v1/activity/sign_in_list';
        $param = [
            "_rx-s" => "mobile"
        ];
        return curl_request($url, $param, $this->bearer());
    }

    /**
     * 领取奖励
     * @return mixed
     * @throws \Exception
     */
    private function sign_in_reward()
    {
        $url   = 'https://member.aliyundrive.com/v1/activity/sign_in_reward';
        $param = [
            'signInDay' => 1
        ];
        return curl_request($url, $param, $this->bearer());
    }

    /**
     * 重新获取token
     * @param $token
     * @return mixed
     * @throws \Exception
     */
    private function refreshToken($token)
    {
        $url   = 'https://auth.aliyundrive.com/v2/account/token';
        $param = [
            "refresh_token" => $token,
            "grant_type"    => "refresh_token"
        ];
        return curl_request($url, $param);
    }

    /**
     * 请求头拼接
     * @return array
     */
    private function bearer()
    {
        return [
            'Authorization:Bearer ' . $this->accessToken,
            'Content-Type:application/json',
            'Origin:https://www.aliyundrive.com',
            'Referer:https://www.aliyundrive.com/'
        ];
    }
}